﻿using System.Text.Json.Serialization;
using System.Text.Json;

namespace CoHubServerCore.Protocol;

internal class JsonInterface
{
    public string Guid { get; }
    public string Source { get; }
    public string Destination { get; }
    public string Date { get; }
    public byte Flag { get; }
    public byte Status { get; }
    public string Type { get; }
    public byte Result { get; }
    public JsonElement Content { get; }

    [JsonConstructor]
    public JsonInterface(string guid, string source, string destination, string date, byte flag, byte status, string type, byte result, JsonElement content)
    {
        Guid = guid;
        Source = source;
        Destination = destination;
        Date = date;
        Flag = flag;
        Status = status;
        Type = type;
        Result = result;
        Content = content;
    }

    public JsonInterface(Packet packet)
    {
        Guid = packet.Guid.ToString("N");
        Source = packet.Source.ToString("N");
        Destination = packet.Destination.ToString("N");
        Date = packet.Data.ToString("yyyy/MM/dd HH:mm:ss");
        Flag = packet.Flag;
        Status = packet.Status;
        Type = packet.Type.ToString();
        Result = packet.Result;
        using var doc = JsonDocument.Parse(JsonSerializer.Serialize(packet.Content));
        Content = doc.RootElement.Clone();
    }
}